use Getopt::Std;
getopt("t:q:n:s:c", \%args);
$tfam = $args{t};
chomp($tfam);
$sampleqc = $args{q};
chomp($sampleqc);
$newsampleqc =  $args{n};
chomp($newsampleqc);
$allsample = $args{s};
chomp($allsample);
$code = $args{c};
chomp($code);
open(WRBUFF,">$newsampleqc");
if($tfam eq "" || $sampleqc eq ""||$newsampleqc eq ""||$allsample eq "")
{
	die "no proper input file\n";	
}

open BUFF,"$tfam" or die "no tfam file found\n";
while($line=<BUFF>)
{
	chomp($line);
	@array = split(" ",$line);
	$hash{$array[1]} = $array[0];
}

open BUFF,"$sampleqc" or die "no file found $sampleqc\n";
$line = <BUFF>;
print WRBUFF $line;
while($line=<BUFF>)
{
        chomp($line);
	#print $line."\n";
	@array = split("\t",$line);
	if(exists($hash{$array[0]}))
	{
		$temp = $array[0];
		$array[0] = $hash{$array[0]};
		#print $hash{$array[0]}."\n";
		$hash2{$hash{$temp}} = 1;
		#print "$hash{$temp}\n";
		$line = join("\t",@array);
		print WRBUFF $line."\n";

	}
	else
	{
		die "samples doesn't match\n";
	}
}
open BUFF,"$allsample" or die "no file found $allsample\n";
$k = 0;
while($line=<BUFF>)
{
        chomp($line);
	#print $line."\n";
	if(exists($hash2{$line}))
	{
		$k++;		
	}
	else
	{
		 @array1 = split('-',$line);
        	#print $array1[3]."\n";
        	@array2 = split(//,$array1[3]);
        	$l = $array2[0].$array2[1];
		#print $l."\n";
		$line = "$line\tN\tNA\tNA\tNA\tNA\tNA\tNA";
        	if($l eq "01" && $code eq "tumor")
        	{
                	print WRBUFF $line."\n";
        	}
		if(($l eq "10" || $l eq "11" || $l eq "12")  && $code eq "normal")
        	{
                	print WRBUFF $line."\n";
        	}

	}
}
if($k == 0)
{
	die "no matching id in the final list\n";
}
